//
//  CarAddModifyViewController.m
//  CPICMobileServiceApp
//
//  Created by sinosoft on 13-2-27.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "CarAddModifyViewController.h"
#import "Config.h"
#import "ASIHTTPRequest.h"
#import "NSString+SBJson.h"
#import "NSObject+SBJson.h"
#import "RCAlertViewUtil.h"
#import "UIImageView+WebCache.h"
#import "NSString+Regex.h"
#import "ASIFormDataRequest.h"
#import "Appdelegate.h"
#import "WSqlite.h"

@interface CarAddModifyViewController ()

@end

@implementation CarAddModifyViewController
@synthesize TableViewCarSetting,TableViewCarInfo,TableViewDate,TableViewVolume,TableViewSkin;
@synthesize txtCarProduct,txtCarBrand,txtCarType,txtCarNum,txtOwnerName,txtLinceNo,txtFirstDate,txtVolume;
@synthesize btnCarProduct,btnCarBrand,btnCarType;
@synthesize progress;
@synthesize strDetailBrand,strDetailProduct,strDetailBrandName,strDetailProductName,strCarID,strCarTypeCode;
@synthesize CarRefreshdelegate;
@synthesize AddorDetail;
@synthesize txtPolicyName,txtLinceType;
@synthesize strManufacturerCode1;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title=@"车辆编辑";
        UIBarButtonItem *rightBtnItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(CarSettingButtonTag)];
        rightBtnItem.title=@"Done";
        self.navigationItem.rightBarButtonItem=rightBtnItem;
        //self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    return self;
}

-(void)CarSettingButtonTag
{
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    ADD_BACKGROUND_BUTTONITEM(@"icon640X88@2x.png");
//    ADD_LEFT_BUTTONITEM(@"back.png");
//    ADD_RIGHTDONE_BUTTONITEM(@"Done.png");
//    ADD_TITLE_NAMEITEM(@"车辆设置");
    
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    NSString *bgimage= [accountDefaults objectForKey:@"UserDefaultBgKindKey"];
    
    if ([bgimage isEqualToString:@"蓝色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88@2x.png");
        ADD_LEFT_BUTTONITEM(@"back.png");
        ADD_RIGHTDONE_BUTTONITEM(@"Done.png");
        ADD_TITLE_NAMEITEM(@"车辆设置");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    else if([bgimage isEqualToString:@"棕色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88_01@2x.png");
        ADD_LEFT_BUTTONITEM(@"back_01.png");
        ADD_RIGHTDONE_BUTTONITEM(@"Done.png");
        ADD_TITLE_NAMEITEM(@"车辆设置");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773_01@2x"]];
    }
    else if([bgimage isEqualToString:@"重大节日皮肤"])
    {
        ADD_RIGHTDONE_BUTTONITEM(@"Done.png");
        NSData *dataImg_02=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_02];
        NSLog(@"-------找到%@",dataImg_02);
        NSData *dataImg_03=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_03];
        NSData *dataImg_04=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_04];
        
        NSURL *urlSkinImg_02=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_02]];
        NSURL *urlSkinImg_03=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_03]];
        NSURL *urlSkinImg_04=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_04]];
        NSLog(@"-------找到333%@",urlSkinImg_02);
        
        UIImageView * LogoImage = [[UIImageView alloc]init];
        [LogoImage setImageWithURL:urlSkinImg_03 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(LogoImage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_03];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        LogoImage.image = [UIImage imageWithData:dataImg_03];
        LogoImage.frame = CGRectMake(0, 0, LogoImage.image.size.width/2, LogoImage.image.size.height/2);
        [self.view addSubview:LogoImage];
        
        //--------------------------
        UIButton * CallOutBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        CallOutBtn1.frame = CGRectMake(10, 10, 109/2, 57/2);
        UIImageView * Back = [[UIImageView alloc]init];
        [Back setImageWithURL:urlSkinImg_02 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(Back.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_02];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [Back setImage:[UIImage imageWithData:dataImg_02]];
        [CallOutBtn1 setImage:Back.image forState:UIControlStateNormal];
        [CallOutBtn1 addTarget:self action:@selector(ClickCallOutBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:CallOutBtn1];
        //--------------------------
        
        ADD_TITLE_NAMEITEM(@"车辆设置");
        
        UIImageView *bgimage = [[UIImageView alloc]initWithFrame:CGRectMake(0,44, self.view.frame.size.width, self.view.frame.size.height-44)];
        [bgimage setImageWithURL:urlSkinImg_04 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(bgimage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_04];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [bgimage setImage:[UIImage imageWithData:dataImg_04]];
        [self.view addSubview:bgimage];
        [bgimage release];
    }

    arrProduct=[[NSMutableArray alloc]init];
    arrProductCode=[[NSMutableArray alloc]init];
    
    arrCarBrand=[[NSMutableArray alloc]init];
    arrCarBrandCode=[[NSMutableArray alloc]init];
    arrManufacturerCode=[[NSMutableArray alloc]init];
    
    arrCarType=[[NSMutableArray alloc]init];
    arrCarName=[[NSMutableArray alloc]init];
    
    txtCarProduct=[[UITextField alloc]initWithFrame:CGRectMake(100, 5, 150, 31)];
    txtCarProduct.borderStyle=UITextBorderStyleLine;
    txtCarProduct.returnKeyType=UIReturnKeyDone;
    txtCarProduct.delegate=self;
    txtCarProduct.enabled=NO;
    
    txtCarBrand=[[UITextField alloc]initWithFrame:CGRectMake(100, 5, 150, 31)];
    txtCarBrand.borderStyle=UITextBorderStyleLine;
    txtCarBrand.returnKeyType=UIReturnKeyDone;
    txtCarBrand.delegate=self;
    txtCarBrand.enabled=NO;
    
    txtCarType=[[UITextField alloc]initWithFrame:CGRectMake(100, 5, 150, 31)];
    txtCarType.borderStyle=UITextBorderStyleLine;
    txtCarType.returnKeyType=UIReturnKeyDone;
    txtCarType.delegate=self;
    txtCarType.enabled=NO;
    
    txtCarNum=[[UITextField alloc]initWithFrame:CGRectMake(135, 5, 165, 31)];
    txtCarNum.borderStyle=UITextBorderStyleRoundedRect;
    txtCarNum.returnKeyType=UIReturnKeyDone;
    txtCarNum.delegate=self;
    
    NSDictionary * dictCustomerInfo =[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_CUSTOMER_INFO];
    if (AddorDetail==YES) {
        if (dictCustomerInfo!=nil)
        {
            if ([dictCustomerInfo valueForKey:@"license"])
            {
                txtCarNum.text=[dictCustomerInfo valueForKey:@"license"];
            }
            
        }
    }
    
    txtOwnerName=[[UITextField alloc]initWithFrame:CGRectMake(135, 5, 165, 31)];
    txtOwnerName.borderStyle=UITextBorderStyleRoundedRect;
    txtOwnerName.returnKeyType=UIReturnKeyDone;
    txtOwnerName.delegate=self;
    
    txtPolicyName=[[UITextField alloc]initWithFrame:CGRectMake(135, 5, 165, 31)];
    txtPolicyName.borderStyle=UITextBorderStyleRoundedRect;
    txtPolicyName.returnKeyType=UIReturnKeyDone;
    txtPolicyName.delegate=self;
    
    txtLinceType=[[UITextField alloc]initWithFrame:CGRectMake(135, 5, 140, 31)];
    txtLinceType.borderStyle=UITextBorderStyleRoundedRect;
    txtLinceType.returnKeyType=UIReturnKeyDone;
    txtLinceType.enabled=NO;
    txtLinceType.delegate=self;
    
    txtLinceNo=[[UITextField alloc]initWithFrame:CGRectMake(135, 5, 165, 31)];
    txtLinceNo.borderStyle=UITextBorderStyleRoundedRect;
    txtLinceNo.returnKeyType=UIReturnKeyDone;
    txtLinceNo.delegate=self;
    txtLinceNo.font=[UIFont systemFontOfSize:13.0];
    
    txtFirstDate=[[UITextField alloc]initWithFrame:CGRectMake(135, 5, 140, 31)];
    txtFirstDate.borderStyle=UITextBorderStyleRoundedRect;
    txtFirstDate.returnKeyType=UIReturnKeyDone;
    txtFirstDate.delegate=self;
    txtFirstDate.enabled=NO;
    
    txtVolume=[[UITextField alloc]initWithFrame:CGRectMake(135, 5, 165, 31)];
    txtVolume.borderStyle=UITextBorderStyleRoundedRect;
    txtVolume.returnKeyType=UIReturnKeyDone;
    txtVolume.delegate=self;
    //添加UITableview
    TableViewCarSetting = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 120) style:UITableViewStylePlain];
    TableViewCarSetting.dataSource = self;
	TableViewCarSetting.delegate = self;
    
    TableViewCarSetting.opaque = NO;
    TableViewCarSetting.backgroundView = nil;
    TableViewCarSetting.autoresizingMask = UIViewContentModeTop;
    TableViewCarSetting.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    TableViewCarSetting.scrollEnabled=NO;
    //TableViewCarSetting.backgroundColor=[UIColor clearColor];
   // [TableViewCarSetting reloadData];
	[self.view addSubview:TableViewCarSetting];
    
    TableViewCarInfo = [[UITableView alloc]initWithFrame:CGRectMake(0, 164, self.view.frame.size.width, 200) style:UITableViewStylePlain];
	TableViewCarInfo.delegate = self;
	TableViewCarInfo.dataSource = self;
    
    TableViewCarInfo.opaque = NO;
    TableViewCarInfo.backgroundView = nil;
    TableViewCarInfo.autoresizingMask = UIViewContentModeTop;
    TableViewCarInfo.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    TableViewCarInfo.scrollEnabled=NO;
    //TableViewCarInfo.backgroundColor=[UIColor clearColor];
    //[TableViewUser reloadData];
	[self.view addSubview:TableViewCarInfo];
    
    TableViewDate = [[UITableView alloc]initWithFrame:CGRectMake(0, 364, self.view.frame.size.width, 40) style:UITableViewStylePlain];
	TableViewDate.delegate = self;
	TableViewDate.dataSource = self;
    
    TableViewDate.opaque = NO;
    TableViewDate.backgroundView = nil;
    TableViewDate.autoresizingMask = UIViewContentModeTop;
    TableViewDate.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    TableViewDate.scrollEnabled=NO;
    //TableViewDate.backgroundColor=[UIColor clearColor];
    //[TableViewUser reloadData];
	[self.view addSubview:TableViewDate];
    
    TableViewVolume = [[UITableView alloc]initWithFrame:CGRectMake(0, 404, self.view.frame.size.width, 40) style:UITableViewStylePlain];
	TableViewVolume.delegate = self;
	TableViewVolume.dataSource = self;
    
    TableViewVolume.opaque = NO;
    TableViewVolume.backgroundView = nil;
    TableViewVolume.autoresizingMask = UIViewContentModeTop;
    TableViewVolume.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    TableViewVolume.scrollEnabled=NO;
    //TableViewVolume.backgroundColor=[UIColor clearColor];
    //[TableViewUser reloadData];
	[self.view addSubview:TableViewVolume];
    
    arrCarSetting=[[NSMutableArray alloc]initWithObjects:@"车辆厂商",@"车辆品牌",@"车辆型号", nil];
    arrCarInfo=[[NSMutableArray alloc]initWithObjects:@"车牌号码",@"车主姓名",@"投保人",@"证件类型",@"证件号码", nil];
    arrLinceType=[[NSMutableArray alloc]initWithObjects:@"身份证",@"军官证",@"护照",@"居民户口薄",@"团体其它", nil];
    dicRequestSave = [[NSMutableDictionary alloc]init];
    dictRequestSQL=[[NSMutableDictionary alloc]init];

    UIButton *btnRegister=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnRegister setTitle:@"保存" forState:UIControlStateNormal];
    btnRegister.frame=CGRectMake(112, 410, 118, 35);
    [btnRegister addTarget:self action:@selector(saveCarInfo) forControlEvents:UIControlEventTouchDown];
    //[self.view addSubview:btnRegister];
    
    [self setManufactureRequest];
}

-(void)saveCarInfo
{
    
    if (self.txtCarProduct.text.length==0 || self.txtCarBrand.text.length==0 || self.txtCarType.text.length==0 || self.txtCarNum.text.length==0 || self.txtOwnerName.text.length==0 || self.txtLinceNo.text.length==0 || self.txtFirstDate.text.length==0) {
        [self justShowMessage:@"请填完整页面信息" title:@"注意"];
        return;
    }
//    if ([self.txtCarNum.text isValidCarLicense]) {
//    }
//    else
//    {
//        [self justShowMessage:@"车牌号码格式不正确" title:@"注意"];
//        return;
//    }
    [self setRequest];
}
-(void) ClickCallOutBtn
{
    [self.CarRefreshdelegate RefreshCarRequest];
    
    [self.navigationController popViewControllerAnimated:YES];
 
    //[self.CarRefreshdelegate RefreshCarRequest];

    
}

-(void)setManufactureRequest//向太保移动平台发送请求报文
{
    WSqlite *sql =[WSqlite Instance];
    NSMutableArray *myArrData=[sql getManufacturer];
    if ([myArrData count]>0) {
        [arrProduct removeAllObjects];
        [arrProductCode removeAllObjects];
        for (int i=0; i<[myArrData count]; i++) {
            
            NSMutableDictionary *dictItem=[myArrData objectAtIndex:i];
            NSString *str=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"manufacturerName"]];
            NSString *strCode=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"manufacturerCode"]];
            [arrProduct addObject:str];
            [arrProductCode addObject:strCode];
        }
    }
    else
    {
        NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
        [dicRequest setObject:@"admin22" forKey:@"userAccount"];
        NSString *strRequestJson=[dicRequest JSONRepresentation];
        ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
        //设置需要POST的数据，这里提交两个数据，A=a&B=b
        [request setPostValue:@"62011" forKey:@"RequestCode"];
        [request setPostValue:strRequestJson forKey:@"RequestData"];
        
        [request setDelegate:self];
        [request setDidFailSelector:@selector(ResponseManufactureFail:)];
        [request setDidFinishSelector:@selector(ResonseManufactureSuccess:)];
        [request startAsynchronous];
        
        showProgress = YES;
        if (showProgress) {
            NSLog(@"88888");
            //[NSThread sleepForTimeInterval:1];
            progress= [[MBProgressHUD alloc]initWithView:self.view];
            [self.view addSubview:progress];
            [self.view bringSubviewToFront:progress];
            progress.labelText = @"查询中...";
            [progress show:YES];
            showProgress = NO;
        };
    }
}

- (void)ResonseManufactureSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    NSMutableArray *myArrData=[dictResponse valueForKey:@"datas"];
    if ([myArrData count]>0) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // 耗时的操作
            WSqlite *sql =[WSqlite Instance];
            [sql insertManufacturer:myArrData];
            dispatch_async(dispatch_get_main_queue(), ^{
                // 更新界面
            });
        });
    }
    [arrProduct removeAllObjects];
    [arrProductCode removeAllObjects];
    for (int i=0; i<[myArrData count]; i++) {

        NSMutableDictionary *dictItem=[myArrData objectAtIndex:i];
        NSString *str=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"manufacturerName"]];
        NSString *strCode=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"manufacturerCode"]];
        [arrProduct addObject:str];
        [arrProductCode addObject:strCode];
    }
    //arrProduct=[dictResponse valueForKey:@"datas"];
    //    NSMutableDictionary *dictAppointmentList=[dictResponse objectForKey:@"reminderServiceInfoList"];
    //    NSMutableArray *myArrData=[dictAppointmentList valueForKey:@"data"];
    //    for (int i=0; i<[myArrData count]; i++) {
    //        NSMutableDictionary *dictItem=[myArrData objectAtIndex:i];
    //        [arrData addObject:dictItem];
    //    }
    //NSLog(@"%@",arrData);
}

- (void)ResponseManufactureFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}

-(void)ClickCallDoneBtn
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    myDelegate.bCarsetting=TRUE;
    
    if (self.txtCarProduct.text.length==0 || self.txtCarBrand.text.length==0 || self.txtCarType.text.length==0 || self.txtCarNum.text.length==0 || self.txtOwnerName.text.length==0 || self.txtLinceNo.text.length==0 || self.txtFirstDate.text.length==0 || self.txtLinceType.text==0 || self.txtPolicyName.text.length==0) {
        [self justShowMessage:@"请填完整页面信息" title:@"注意"];
        return;
    }
    
    NSString *strCarNumLeftTwo=self.txtCarNum.text;
    strCarNumLeftTwo=[strCarNumLeftTwo substringToIndex:2];
    NSLog(@"strCarNumLeftTwo:%@",strCarNumLeftTwo);
//    if ([self.txtCarNum.text isValidCarLicense]) {
//    }
//    else if([strCarNumLeftTwo isEqualToString:@"粤Z"])
//    {
//        //粤Z香港车牌的先不做判断，让它通过验证
//    }
//    else
//    {
//        [self justShowMessage:@"车牌号码格式不正确" title:@"注意"];
//        return;
//    }
    [self setRequest];
}
#pragma UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView==TableViewCarSetting)
    {
         return [arrCarSetting count];
    }
    else if(tableView==TableViewCarInfo)
    {
        return [arrCarInfo count];
    }
    else
    {
        return 1;
    }
   
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIButton *btnCarNum = [UIButton buttonWithType:UIButtonTypeCustom];
    btnCarNum.frame = CGRectMake(100.0f, 10.0f, 49/2, 54/2);
    [btnCarNum setImage:[UIImage imageNamed:@"Info.png"] forState:UIControlStateNormal];

    
    if(tableView==TableViewCarSetting)
    {
        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewCarSetting dequeueReusableCellWithIdentifier:Identifier];

        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中后颜色没变化
            
        }
        UILabel *lblCar=[[UILabel alloc]initWithFrame:CGRectMake(20, 13, 210, 21)];
        lblCar.backgroundColor=[UIColor clearColor];
        NSString *strCar=[arrCarSetting objectAtIndex:indexPath.row];
        lblCar.text=strCar;
        [cell addSubview:lblCar];
        [lblCar release];
        
        UIImage *image= [ UIImage imageNamed:@"login_type.png" ];
        UIButton *button = [ UIButton buttonWithType:UIButtonTypeCustom ];
        CGRect frame = CGRectMake( 25.0 , 5.0 , 58/2 , 49/2 );
        button. frame = frame;
        [button setBackgroundImage:image forState:UIControlStateNormal ];
        button. backgroundColor = [UIColor clearColor ];
        if(indexPath.row==0)
        {

            [cell addSubview:txtCarProduct];
            [button addTarget:self action:@selector(didSelctCustomActionSheetProduct) forControlEvents:UIControlEventTouchUpInside];
            cell. accessoryView = button;
        }
        else if(indexPath.row==1)
        {

            [cell addSubview:txtCarBrand];
            [button addTarget:self action:@selector(didSelctCustomActionSheetBrand) forControlEvents:UIControlEventTouchUpInside];
            cell. accessoryView = button;
        }
        else
        {
            [cell addSubview:txtCarType];
            [button addTarget:self action:@selector(didSelctCustomActionSheetType) forControlEvents:UIControlEventTouchUpInside];
            cell. accessoryView = button;
        }
        return cell;

    }
    else if(tableView==TableViewCarInfo)
    {
        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewCarInfo dequeueReusableCellWithIdentifier:Identifier];
        
        UIImage *image= [ UIImage imageNamed:@"login_type.png" ];
        UIButton *button = [ UIButton buttonWithType:UIButtonTypeCustom ];
        CGRect frame = CGRectMake( 25.0 , 5.0 , 58/2 , 49/2 );
        button. frame = frame;
        [button setBackgroundImage:image forState:UIControlStateNormal ];
        button. backgroundColor = [UIColor clearColor ];
        
        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中后颜色没变化
        }
        UILabel *lblCarInfo=[[UILabel alloc]initWithFrame:CGRectMake(20, 13, 210, 21)];
        lblCarInfo.backgroundColor=[UIColor clearColor];
        NSString *strCarInfo=[arrCarInfo objectAtIndex:indexPath.row];
        lblCarInfo.text=strCarInfo;
        [cell addSubview:lblCarInfo];
        [lblCarInfo release];
        
        if(indexPath.row==0)
        {

            [cell addSubview:txtCarNum];
            [cell addSubview:btnCarNum];
            btnCarNum.tag=indexPath.row;
            [btnCarNum addTarget:self action:@selector(getCarInfo:) forControlEvents:UIControlEventTouchUpInside];
        }
        else if(indexPath.row==1)
        {

            [cell addSubview:txtOwnerName];
            [cell addSubview:btnCarNum];
            btnCarNum.tag=indexPath.row;
            [btnCarNum addTarget:self action:@selector(getNameInfo:) forControlEvents:UIControlEventTouchUpInside];
        }
        else if(indexPath.row==2)
        {
            [cell addSubview:txtPolicyName];
            [cell addSubview:btnCarNum];
            btnCarNum.tag=indexPath.row;
            [btnCarNum addTarget:self action:@selector(getPolicyName:) forControlEvents:UIControlEventTouchUpInside];
        }
        else if(indexPath.row==3)
        {
            [cell addSubview:txtLinceType];
            [cell addSubview:btnCarNum];
            btnCarNum.tag=indexPath.row;
            [btnCarNum addTarget:self action:@selector(getLinceType:) forControlEvents:UIControlEventTouchUpInside];
            [button addTarget:self action:@selector(didSelctCustomActionSheetLinceType) forControlEvents:UIControlEventTouchUpInside];
            cell. accessoryView = button;
        }
        else
        {
            [cell addSubview:txtLinceNo];
            [cell addSubview:btnCarNum];
            btnCarNum.tag=indexPath.row;
            [btnCarNum addTarget:self action:@selector(getLinceInfo:) forControlEvents:UIControlEventTouchUpInside];
        }
        return cell;

    }
    else if(tableView==TableViewDate)
    {
        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewDate dequeueReusableCellWithIdentifier:Identifier];
        
        UIImage *image= [ UIImage imageNamed:@"login_type.png" ];
        UIButton *button = [ UIButton buttonWithType:UIButtonTypeCustom ];
        CGRect frame = CGRectMake( 25.0 , 5.0 , 58/2 , 49/2 );
        button. frame = frame;
        [button setBackgroundImage:image forState:UIControlStateNormal ];
        button. backgroundColor = [UIColor clearColor ];

        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中后颜色没变化
            [button addTarget:self action:@selector(didSelectCustomDateActionSheet) forControlEvents:UIControlEventTouchUpInside];
            cell.accessoryView = button;
        }
        UILabel *lblCarInfo=[[UILabel alloc]initWithFrame:CGRectMake(20, 13, 210, 21)];
        lblCarInfo.backgroundColor=[UIColor clearColor];
        NSString *strCarInfo=@"初登日期";
        lblCarInfo.text=strCarInfo;
        [cell addSubview:lblCarInfo];
        [lblCarInfo release];
        

        [cell addSubview:txtFirstDate];
        [cell addSubview:btnCarNum];
        
        btnCarNum.tag=indexPath.row;
        [btnCarNum addTarget:self action:@selector(getFirstDateInfo:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }
    else
    {
        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewVolume dequeueReusableCellWithIdentifier:Identifier];
        
        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中后颜色没变化
        }
//        UILabel *lblCarInfo=[[UILabel alloc]initWithFrame:CGRectMake(20, 13, 210, 21)];
//        lblCarInfo.backgroundColor=[UIColor clearColor];
//        NSString *strCarInfo=@"排  量";
//        lblCarInfo.text=strCarInfo;
//        [cell addSubview:lblCarInfo];
//        [lblCarInfo release];
//        
//
//        [cell addSubview:txtVolume];
//        [cell addSubview:btnCarNum];
        
        return cell;
    }

      
}
-(void)getCarInfo:(id)sender
{
    [self justShowMessage:@"该项信息为您参与我司相关客户活动的辨识信息." title:@"提示"];
}
-(void)getNameInfo:(id)sender
{
    [self justShowMessage:@"车主姓名。" title:@"提示"];
}
-(void)getLinceInfo:(id)sender
{
    [self justShowMessage:@"证件号码。" title:@"提示"];
}
-(void)getFirstDateInfo:(id)sender
{
     [self justShowMessage:@"该项信息提醒您为爱车提前续保，防范爱车脱保所带来的未知风险." title:@"提示"];
}
-(void)getPolicyName:(id)sender
{
    [self justShowMessage:@"投保人" title:@"提示"];
}
-(void)getLinceType:(id)sender
{
    [self justShowMessage:@"证件类型" title:@"提示"];
}
-(void)getDetailInfo:(id)sender
{
    UIButton *btn=(UIButton *)sender;
    NSLog(@"%d",btn.tag);
    if (btn.tag==3) {
        [self justShowMessage:@"该项信息为您参与我司相关客户活动的辨识信息." title:@"提示"];
    }
    else if(btn.tag==4)
    {
        [self justShowMessage:@"您所录入的相关个人及信息，仅为使用软件相应功能所必须，不做任何商业用途。" title:@"提示"];
    }
    else if(btn.tag==5)
    {
        [self justShowMessage:@"您所录入的相关个人及信息，仅为使用软件相应功能所必须，不做任何商业用途。" title:@"提示"];
    }
    else if(btn.tag==6)
    {
         [self justShowMessage:@"该项信息提醒您为爱车提前续保，防范爱车脱保所带来的未知风险." title:@"提示"];
    }
    
}
-(void)didSelctCustomActionSheetProduct
{
    if ([arrProduct count]==0) {
        [self justShowMessage:@"生产厂商为空" title:@"注意"];
        return;
    }
    CustomActionSheet *sheet=[[CustomActionSheet alloc]initWithHeight:284.0f WithSheetTitle:@"" WithDataArray:arrProduct];
    sheet.tag=0;
    sheet.Customdelegate=self;
    [sheet showInView:self.view];
}
-(void)didSelctCustomActionSheetBrand
{
    if ([arrCarBrand count]==0) {
        [self justShowMessage:@"车辆品牌为空" title:@"注意"];
        return;
    }
    CustomActionSheet *sheet=[[CustomActionSheet alloc]initWithHeight:284.0f WithSheetTitle:@"" WithDataArray:arrCarBrand];
    sheet.tag=1;
    sheet.Customdelegate=self;
    [sheet showInView:self.view];
}
-(void)didSelctCustomActionSheetType
{
    if ([arrCarName count]==0) {
        [self justShowMessage:@"车辆型号为空" title:@"注意"];
        return;
    }
    CustomActionSheet *sheet=[[CustomActionSheet alloc]initWithHeight:284.0f WithSheetTitle:@"" WithDataArray:arrCarName];
    sheet.tag=2;
    sheet.Customdelegate=self;
    [sheet showInView:self.view];
}
-(void)didSelectCustomDateActionSheet
{
    CustomDateActionSheet *dateActionSheet=[[CustomDateActionSheet alloc]initWithDateOnlyActionHeight:284.0f WithSheetTitle:nil];
    dateActionSheet.tag=0;
    dateActionSheet.CustomDateDelegate=self;
    [dateActionSheet showInView:self.view];
    
}
-(void)didSelctCustomActionSheetLinceType
{
    CustomActionSheet *sheet=[[CustomActionSheet alloc]initWithHeight:284.0f WithSheetTitle:@"" WithDataArray:arrLinceType];
    sheet.tag=3;
    sheet.Customdelegate=self;
    [sheet showInView:self.view];
}

-(void)ClickDate:(NSString *)Date DateActionSheetTag:(NSInteger)dateActionSheetTag
{
    if (dateActionSheetTag==0) {
        self.txtFirstDate.font=[UIFont systemFontOfSize:14.0];
        self.txtFirstDate.text=[Date substringToIndex:10];
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (tableView==TableViewCarSetting ) {
//        if(indexPath.row==0)
//        {
//            if ([arrProduct count]==0) {
//                [self justShowMessage:@"生产厂家为空" title:@"注意"];
//                return;
//            }
//            CustomActionSheet *sheet=[[CustomActionSheet alloc]initWithHeight:284.0f WithSheetTitle:@"" WithDataArray:arrProduct];
//            sheet.tag=0;
//            sheet.Customdelegate=self;
//            [sheet showInView:self.view];
//        }
//        else if(indexPath.row==1)
//        {
////            CustomActionSheet *sheet=[[CustomActionSheet alloc]initWithHeight:284.0f WithSheetTitle:@"" WithDataArray:arrCarBrand];
////            sheet.tag=1;
////            sheet.Customdelegate=self;
////            [sheet showInView:self.view];
//        }
//        else if(indexPath.row==2)
//        {
////            CustomActionSheet *sheet=[[CustomActionSheet alloc]initWithHeight:284.0f WithSheetTitle:@"" WithDataArray:arrCarType];
////            sheet.tag=2;
////            sheet.Customdelegate=self;
////            [sheet showInView:self.view];
//        }
//    }

}
//-(void)ClickItem:(NSString *)Title ActionSheetTag:(NSInteger)actionSheetTag
//{
//    if (actionSheetTag==0) {
//        self.txtCarProduct.text=Title;
//    }
//    else if(actionSheetTag==1)
//    {
//        self.txtCarBrand.text=Title;
//    }
//    else if(actionSheetTag==2)
//    {
//        self.txtCarType.text=Title;
//    }
//}

-(void)ClickItem:(NSString *)Title selectRow:(NSInteger)SelectRow ActionSheetTag:(NSInteger)actionSheetTag
{
    if (actionSheetTag==0) {
        self.txtCarProduct.text=Title;
        self.txtCarBrand.text=@"";
        self.txtCarType.text=@"";
        if (self.txtCarProduct.text.length!=0) {
            strManufacturerCode1=[arrProductCode objectAtIndex:SelectRow];
            if (strManufacturerCode1.length!=0) {
                [self setBrandRequest:strManufacturerCode1];
            }
            else
            {
                [self justShowMessage:@"生产厂商代码为空" title:@"注意"];
                return;
            }
        }
    }
    else if(actionSheetTag==1)
    {
        self.txtCarBrand.text=Title;
        self.txtCarType.text=@"";
        if (self.txtCarBrand.text.length!=0) {
            strBrandCode1=[arrCarBrandCode objectAtIndex:SelectRow];
            if (strBrandCode1.length!=0) {
                [self setTypeRequest:strBrandCode1];
            }
            else
            {
                [self justShowMessage:@"车辆品牌代码为空" title:@"注意"];
                return;
            }
        }
    }
    else if(actionSheetTag==2)
    {
        
        self.txtCarType.text=Title;
        if (self.txtCarType.text.length!=0) {
            strCarTypeCode=[arrCarType objectAtIndex:SelectRow];
        }
        else
        {
            [self justShowMessage:@"车辆型号代码为空" title:@"注意"];
            return;
        }
    }
    else if(actionSheetTag==3)
    {
        self.txtLinceType.text=Title;
    }
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField==txtCarNum)
    {
        [self SetAnimation:80];
    }
    else if(textField==txtOwnerName)
    {
        [self SetAnimation:100];
    }
    else if(textField==txtPolicyName)
    {
        [self SetAnimation:120];
    }
    else if(textField==txtLinceType)
    {
        [self SetAnimation:150];
    }
    else if(textField==txtLinceNo)
    {
        [self SetAnimation:180];
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if(textField==txtCarNum)
    {
        [self backAnimation:80];
    }
    else if(textField==txtOwnerName)
    {
        [self backAnimation:100];
    }
    else if(textField==txtPolicyName)
    {
        [self backAnimation:120];
    }
    else if(textField==txtLinceType)
    {
        [self backAnimation:150];
    }
    else if(textField==txtLinceNo)
    {
        [self backAnimation:180];
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField==txtCarProduct)
    {
        if(range.location>=30)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtCarBrand)
    {
        if(range.location>=30)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtCarType)
    {
        if(range.location>=30)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtCarNum)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtOwnerName)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtLinceNo)
    {
        if(range.location>=18)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtPolicyName)
    {
        if(range.location>=30)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtLinceType)
    {
        if(range.location>=30)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)SetAnimation:(CGFloat)Xorigin
{
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    //frame.origin.x+=Xorigin;
    frame.origin.y-=Xorigin;
    //frame.size.height +=Xorigin;
    self.view.frame = frame;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

-(void)backAnimation:(CGFloat)Xorigin
{
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    //frame.origin.x -=Xorigin;
    frame.origin.y+=Xorigin;
    //frame.size.height -=Xorigin;
    self.view.frame = frame;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
    // [textField resignFirstResponder];
}

-(void)setTypeRequest:(NSString *)strTypeCode//向太保移动平台发送请求报文
{
//    NSString *strRequestCode=@"RequestCode=62013";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
    [dicRequest setObject:@"admin22" forKey:@"userAccount"];
    [dicRequest setObject:strManufacturerCode1 forKey:@"manufacturerCode"];
    [dicRequest setObject:strTypeCode forKey:@"brandCode"];
    NSString *strRequestJson=[dicRequest JSONRepresentation];
//    strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [request setPostValue:@"62013" forKey:@"RequestCode"];
    [request setPostValue:strRequestJson forKey:@"RequestData"];
    
    [request setDelegate:self];
    [request setDidFailSelector:@selector(ResponseTypeFail:)];
    [request setDidFinishSelector:@selector(ResonseTypeSuccess:)];
    [request startAsynchronous];
    
    showProgress = YES;
    if (showProgress) {
        NSLog(@"88888");
        //[NSThread sleepForTimeInterval:1];
        progress= [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:progress];
        [self.view bringSubviewToFront:progress];
        progress.labelText = @"查询中...";
        [progress show:YES];
        showProgress = NO;
    };
    
    
}

- (void)ResonseTypeSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    NSMutableArray *myArrData=[dictResponse valueForKey:@"datas"];
    [arrCarType removeAllObjects];
    [arrCarName removeAllObjects];
    for (int i=0; i<[myArrData count]; i++) {
        
        NSMutableDictionary *dictItem=[myArrData objectAtIndex:i];
        NSString *str=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"carType"]];
        NSString *strCarName=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"carName"]];
        [arrCarType addObject:str];
        [arrCarName addObject:strCarName];
        [str release];
        [strCarName release];
    }
    //arrProduct=[dictResponse valueForKey:@"datas"];
    //    NSMutableDictionary *dictAppointmentList=[dictResponse objectForKey:@"reminderServiceInfoList"];
    //    NSMutableArray *myArrData=[dictAppointmentList valueForKey:@"data"];
    //    for (int i=0; i<[myArrData count]; i++) {
    //        NSMutableDictionary *dictItem=[myArrData objectAtIndex:i];
    //        [arrData addObject:dictItem];
    //    }
    //NSLog(@"%@",arrData);
}

- (void)ResponseTypeFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}



-(void)setBrandRequest:(NSString *)strManufacturerCode//向太保移动平台发送请求报文
{
    WSqlite *sql =[WSqlite Instance];
    NSMutableArray *myArrData=[sql getBrand:strManufacturerCode];
    if ([myArrData count]>0) {
        [arrCarBrand removeAllObjects];
        [arrCarBrandCode removeAllObjects];
        [arrManufacturerCode removeAllObjects];
        for (int i=0; i<[myArrData count]; i++) {
            
            NSMutableDictionary *dictItem=[myArrData objectAtIndex:i];
            NSString *str=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"brandName"]];
            NSString *strCode=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"brandCode"]];
            NSString *strManufacturerCode=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"manufacturerCode"]];
            [arrCarBrand addObject:str];
            [arrCarBrandCode addObject:strCode];
            [arrManufacturerCode addObject:strManufacturerCode];
            [str release];
            [strCode release];
            [strManufacturerCode release];
        }
    }
    else
    {
        NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
        [dicRequest setObject:@"admin22" forKey:@"userAccount"];
        [dicRequest setObject:strManufacturerCode forKey:@"manufacturerCode"];
        NSString *strRequestJson=[dicRequest JSONRepresentation];
        ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
        //设置需要POST的数据，这里提交两个数据，A=a&B=b
        [request setPostValue:@"62012" forKey:@"RequestCode"];
        [request setPostValue:strRequestJson forKey:@"RequestData"];
        
        [request setDelegate:self];
        [request setDidFailSelector:@selector(ResponseBrandFail:)];
        [request setDidFinishSelector:@selector(ResonseBrandSuccess:)];
        [request startAsynchronous];
        
        showProgress = YES;
        if (showProgress) {
            NSLog(@"88888");
            //[NSThread sleepForTimeInterval:1];
            progress= [[MBProgressHUD alloc]initWithView:self.view];
            [self.view addSubview:progress];
            [self.view bringSubviewToFront:progress];
            progress.labelText = @"查询中...";
            [progress show:YES];
            showProgress = NO;
        };
    }    
    
}

- (void)ResonseBrandSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    NSMutableArray *myArrData=[dictResponse valueForKey:@"datas"];
    if ([myArrData count]>0) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // 耗时的操作
            if (strManufacturerCode1.length!=0) {
                WSqlite *sql =[WSqlite Instance];
                [sql insertBrand:myArrData withManufacturerCode:strManufacturerCode1];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                // 更新界面
            });
        });
    }
    [arrCarBrand removeAllObjects];
    [arrCarBrandCode removeAllObjects];
    [arrManufacturerCode removeAllObjects];
    for (int i=0; i<[myArrData count]; i++) {
        
        NSMutableDictionary *dictItem=[myArrData objectAtIndex:i];
        NSString *str=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"brandName"]];
        NSString *strCode=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"brandCode"]];
        NSString *strManufacturerCode=[[NSString alloc]initWithFormat:@"%@",[dictItem valueForKey:@"manufacturerCode"]];
        [arrCarBrand addObject:str];
        [arrCarBrandCode addObject:strCode];
        [arrManufacturerCode addObject:strManufacturerCode];
        [str release];
        [strCode release];
        [strManufacturerCode release];
    }
    //arrProduct=[dictResponse valueForKey:@"datas"];
    //    NSMutableDictionary *dictAppointmentList=[dictResponse objectForKey:@"reminderServiceInfoList"];
    //    NSMutableArray *myArrData=[dictAppointmentList valueForKey:@"data"];
    //    for (int i=0; i<[myArrData count]; i++) {
    //        NSMutableDictionary *dictItem=[myArrData objectAtIndex:i];
    //        [arrData addObject:dictItem];
    //    }
    //NSLog(@"%@",arrData);
}

- (void)ResponseBrandFail:(ASIHTTPRequest *)request
{
    [arrCarBrand removeAllObjects];
    [arrCarBrandCode removeAllObjects];
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}

-(void)setRequest//向太保移动平台发送请求报文
{
    NSLog(@"userinfodefault%@",[[NSUserDefaults standardUserDefaults] objectForKey:DICT_USER_INFO]);
    NSDictionary *dictUserInfo=[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_USER_INFO];
    if (dictUserInfo!=nil) {
        if ([dictUserInfo valueForKey:@"UserAccount"]) {
            NSLog(@"%@",[dictUserInfo valueForKey:@"UserAccount"]);
        }
    }
    else
    {
        [self justShowMessage:@"用户还没登录,请登录系统" title:@"注意"];
        return;
    }
    
    
//    NSString *strRequestCode=@"RequestCode=60201";
    //[dicRequest setObject:@"1" forKey:@"addorModifyTag"];
    if(strBrandCode1.length!=0)
    {
        
            [dicRequestSave setObject:strBrandCode1 forKey:@"brand"];
    }
    else
    {
        if ([txtCarBrand.text isEqualToString:strDetailBrandName]) {
            [dicRequestSave setObject:strDetailBrand forKey:@"brand"];
        }
        else
        {
            [dicRequestSave setObject:@"" forKey:@"brand"];
        }
    }
    
    if (self.txtFirstDate.text.length!=0)
    {
         [dicRequestSave setObject:[self.txtFirstDate.text substringToIndex:10] forKey:@"firstDate"];
    }
    else
    {
        [dicRequestSave setObject:@"" forKey:@"firstDate"];
    }
   
    if(self.txtLinceNo.text.length!=0)
    {
        [dicRequestSave setObject:self.txtLinceNo.text forKey:@"iDNum"];
    }
    else
    {
         [dicRequestSave setObject:@"" forKey:@"iDNum"];
    }
    
    if (self.txtCarNum.text.length!=0)
    {
         [dicRequestSave setObject:self.txtCarNum.text forKey:@"license"];
    }
    else
    {
        [dicRequestSave setObject:@"" forKey:@"license"];
    }
   
    if (strManufacturerCode1.length!=0)
    {
        [dicRequestSave setObject:strManufacturerCode1 forKey:@"manufacturers"];
    }
    else
    {
        if ([txtCarProduct.text isEqualToString:strDetailProductName]) {
            [dicRequestSave setObject:strDetailProduct forKey:@"manufacturers"];
        }
        else
        {
            [dicRequestSave setObject:@"" forKey:@"manufacturers"];
        }
    }
    
    if(self.txtOwnerName.text.length!=0)
    {
         [dicRequestSave setObject:self.txtOwnerName.text forKey:@"name"];
    }
    else
    {
         [dicRequestSave setObject:@"" forKey:@"name"];
    }
   
    if(strCarTypeCode.length!=0)
    {
        [dicRequestSave setObject:strCarTypeCode forKey:@"type"];
    }
    else
    {
        [dicRequestSave setObject:@"" forKey:@"type"];
    }
    
    if (strCarID.length!=0) {
        [dicRequestSave setObject:strCarID forKey:@"carID"];
    }
    if ([self.txtLinceType.text isEqualToString:@"身份证"]) {
        [dicRequestSave setObject:@"1" forKey:@"idType"];
    }else if ([self.txtLinceType.text isEqualToString:@"军官证"])
    {
         [dicRequestSave setObject:@"2" forKey:@"idType"];
    }else if ([self.txtLinceType.text isEqualToString:@"护照"])
    {
        [dicRequestSave setObject:@"4" forKey:@"idType"];
    }else if ([self.txtLinceType.text isEqualToString:@"居民户口薄"])
    {
        [dicRequestSave setObject:@"5" forKey:@"idType"];
    }else if ([self.txtLinceType.text isEqualToString:@"团体其它"])
    {
        [dicRequestSave setObject:@"999" forKey:@"idType"];
    }else
    {
         [dicRequestSave setObject:@"" forKey:@"idType"];
    }
    
    if(self.txtPolicyName.text.length!=0)
    {
        [dicRequestSave setObject:self.txtPolicyName.text forKey:@"policyHolder"];
    }
    else
    {
        [dicRequestSave setObject:@"" forKey:@"policyHolder"];
    }
    [dicRequestSave setObject:[dictUserInfo valueForKey:@"UserAccount"] forKey:@"userAccount"];
    dictRequestSQL=dicRequestSave;
    [dictRequestSQL setObject:txtCarProduct.text forKey:@"manufacturersName"];
    [dictRequestSQL setObject:txtCarBrand.text forKey:@"brandName"];
    [dictRequestSQL setObject:txtCarType.text forKey:@"typeName"];
    NSString *strRequestJson=[dicRequestSave JSONRepresentation];
    
    //strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
    NSLog(@"车辆设置的报文体为:%@",strRequestJson);
     ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [request setPostValue:@"60201" forKey:@"RequestCode"];
    [request setPostValue:strRequestJson forKey:@"RequestData"];
    
    [request setDelegate:self];
    [request setDidFailSelector:@selector(ResponseFail:)];
    [request setDidFinishSelector:@selector(ResonseSuccess:)];
    [request startAsynchronous];
    
    showProgress = YES;
    if (showProgress) {
        NSLog(@"88888");
        //[NSThread sleepForTimeInterval:1];
        progress= [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:progress];
        [self.view bringSubviewToFront:progress];
        progress.labelText = @"查询中...";
        [progress show:YES];
        showProgress = NO;
    }
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    NSMutableDictionary *dictHead=[dictResponse valueForKey:@"head"];
    NSString *strResult= [[NSString alloc] initWithFormat:@"%@",[dictHead objectForKey:@"success"]];
    NSLog(@"strResult%@",strResult);
    if ([strResult isEqualToString:@"1"]) {
        
        WSqlite *sql =[WSqlite Instance];
        NSDictionary *dictUserInfo=[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_USER_INFO];
        [sql insertCarDetail:dicRequestSave withCarID:strCarID withUserAccouont:[dictUserInfo valueForKey:@"UserAccount"]];
        
        
        [self justShowMessage:@"保存成功" title:nil];
        NSDictionary *dictCarInfo=[[NSDictionary alloc]initWithObjectsAndKeys:self.txtCarNum.text,@"license",self.txtFirstDate.text,@"firstdate", nil];
        [[NSUserDefaults standardUserDefaults] setObject:dictCarInfo forKey:DICT_CAR_INFO];
        [self.CarRefreshdelegate RefreshCarRequest];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self justShowMessage:[dictHead objectForKey:@"message"] title:nil];
    }
    [strResult release];
    
}

- (void)ResponseFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)dealloc
{
//    [dicRequestSave release];
//    [dictRequestSQL release];
    [arrProduct release];
    [arrProductCode release];
    
    [arrCarBrand release];
    [arrCarBrandCode release];
    [arrManufacturerCode release];
    [arrCarType release];
    [arrCarName release];

    [arrCarInfo release];
    [arrCarSetting release];
    [arrLinceType release];

    [txtCarProduct release];
    [txtCarBrand release];
    [txtCarType release];
    [txtCarNum release];
    [txtOwnerName release];
    [txtLinceType release];
    [txtPolicyName release];
    [txtLinceNo release];
    [txtFirstDate release];
    
    [TableViewCarSetting release];
    [super dealloc];
}

@end
